package com.jic.product.controller.manage;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.manage.request.CategoryTreeNodeAddProductRequest;
import com.jic.product.manage.request.CategoryTreeNodeProductRequest;
import com.jic.product.manage.response.CategoryTreeNodeProductResponse;
import com.jic.product.service.CategoryTreeNodeProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by ZXK on 2020/1/9 11:04
 * 类目聚合商品表
 */
@Slf4j
@RestController
@RequestMapping("/categoryTreeNodeProduct")
@Api(value = "类目聚合商品", description = "类目聚合商品", tags = "类目聚合商品")
public class CategoryTreeNodeProductController {

    @Autowired
    private CategoryTreeNodeProductService categoryTreeNodeProductService;


    /*
     * 分页查询
     * */
    @ApiOperation(value = "获取聚合商品信息")
    @ResponseBody
    @PostMapping(value = "/queryPage")
    public RestResult<PageResult<CategoryTreeNodeProductResponse>> queryPage(@RequestBody CategoryTreeNodeProductRequest form) {
        log.info("开始调用获取聚合商品信息=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        PageResult<CategoryTreeNodeProductResponse> result= categoryTreeNodeProductService.queryPage(form, page);
        PageResult<CategoryTreeNodeProductResponse> pageResult = new PageResult<>();
        pageResult.setRows(result.getRows());
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);

    }



    /*
     *  类目商品聚合新增
     *
     * */
    @ResponseBody
    @PostMapping(value = "/insertCategoryTreeNodeProduct")
    public RestResult insertCategoryTreeNodeProduct(@RequestBody CategoryTreeNodeAddProductRequest categoryTreeNodeAddProductRequest) {
        log.info("开始类目商品聚合新增新增=========》,{}", categoryTreeNodeAddProductRequest.toString());
        RestResult restResult = new RestResult<>();
        try {
//            CategoryTreeNodeProduct categoryTreeNodeProduct=BeanUtil.copy(categoryTreeNodeProductRequest,CategoryTreeNodeProduct.class);
             categoryTreeNodeProductService.insert(categoryTreeNodeAddProductRequest);
              return RestResult.success(true);
        } catch (Exception e) {
            log.error("insertCategoryTreeNodeProduct========》",e);
            restResult=RestResult.error("9999",e.getLocalizedMessage());
        }
            return restResult;

    }


    /*
     *  删除已聚合的列表
     *
     * */
    @ResponseBody
    @PostMapping(value = "/deleteCategoryTreeNodeProduct")
    public RestResult deleteCategoryTreeNodeProduct(@RequestBody CategoryTreeNodeProductRequest  request) {
        log.info("开始调用删除已聚合商品的列表=========》,{}", request.toString());
        RestResult restResult = new RestResult<>();
        try {
           String[] arrIds=request.getIds().split(",");
           for(int i=0;i<arrIds.length;i++){
               categoryTreeNodeProductService.delete(Long.valueOf(arrIds[i]));
           }
           return  RestResult.success(true);
        } catch (Exception e) {
            log.error("deleteCategoryTreeNodeRelation========》",e);
        }
        return restResult;

    }
}
